package com.accounting.client.frontend.product.handlers;

import com.accounting.client.remote.obj.PurchaseRecordItem;
import com.accounting.client.remote.obj.Stock;
import com.accounting.client.resource.BoxResources;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.delegate.Action;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.panel.SimpleLayoutPanel;

public class CreateStockHandler extends SimpleDialogHandler {
	
	private Action action;
	private ScrollPanel summaryPanel;
	private Stock stock;
	
	public CreateStockHandler(Stock stock, Action action) {
		this.action = action;
		this.stock  = stock;
	}
	
	@Override
	public String getHeading() {
		return "增加库存";
	}

	@Override
	public Size getSize() {
		return new Size(1200, 600);
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[]{
			new IsButton("关闭", BoxResources.INSTANCE.cancel16(), 100, new WidgetListener(){
				@Override
				public void valueUpdated(Widget widget) {
					closeDialog();
				}
			})
		};
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}
	
	public Stock getStock() {
		return this.stock;
	}
	
	@Override
	public boolean isCloseable() {
		return false;
	}

	@Override
	public Widget getContentPanel() {
		summaryPanel = createSummaryPanel();
		summaryPanel.getElement().getStyle().setPadding(20, Unit.PX);
		
		SimpleLayoutPanel layout = new SimpleLayoutPanel();
		layout.add(summaryPanel);
		layout.setWidgetTopBottom(summaryPanel, 0, Unit.PX, 0, Unit.PX);
		return layout;
	}
	
	private ScrollPanel createSummaryPanel() {
		PurchaseRecordItem productRecordItem  = new PurchaseRecordItem();
		productRecordItem.setProductId(getStock().getProductId());
		productRecordItem.setProductName(getStock().getProductName());
		productRecordItem.setLocation(getStock().getLocation());
		
		ScrollPanel summaryPanel = new ScrollPanel();
		summaryPanel.setWidget(new CreatePurchaseRecordPanel(productRecordItem, new Action() {
			@Override
			public void run() {
				closeDialog();
				if (action != null){
					action.run();	
				}
			}
		}));
		
		return summaryPanel;
	}
	
	

	
}
